package com.bwhx.eruler.dao.casdilly.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Repository;

import com.bwhx.eruler.bean.PCustomer;
import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.casdilly.CasdillyCustomer;
import com.bwhx.eruler.bean.casdilly.CasdillyScanData;
import com.bwhx.eruler.bean.casdilly.Linkman;
import com.bwhx.eruler.dao.casdilly.CasdillyCustomerDAO;
import com.bwhx.eruler.dao.impl.BaseDaoImpl;
import com.bwhx.eruler.util.StringUtils;
import com.mysql.jdbc.log.Log;
@Repository
public class CustomerDAOImpl extends BaseDaoImpl<CasdillyCustomer> implements CasdillyCustomerDAO {
	
	public CustomerDAOImpl() {
		this.setNs("com.bwhx.eruler.mapper.casdilly.CustomerMapper.");
	}



	@Override
	public List<CasdillyCustomer> findByFenchangIDAndCreatorID(String lMIDFenChang, String loginID) {
		
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("fenchang", lMIDFenChang);
		paramMap.put("loginID", loginID);
		return getSqlSession().selectList(this.getNs() + "findByFenchangIDAndCreatorID", paramMap);
	}

	@Override
	public List<CasdillyCustomer> findUserDataByFenchangIDAndCreatorID(String lMIDFenChang, String loginID) {
		
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("fenchang", lMIDFenChang);
		paramMap.put("loginID", loginID);
		return getSqlSession().selectList(this.getNs() + "findUserDataByFenchangIDAndCreatorID", paramMap);
	}



	@Override
	public String addCustomer(CasdillyCustomer customer) {
		getSqlSession().insert(this.getNs() + "save", customer);
		String customerID = customer.getCustomerID().toString();
		return customerID;
	}



	@Override
	public int updateCustomerPic(HashMap<String, String> dataMap) {
		int update = getSqlSession().update(this.getNs() + "updateCustomerPic", dataMap);
		return update;
	}



	@Override
	public CasdillyCustomer findByID(String customerID) {
		List<Object> selectList = getSqlSession().selectList(this.getNs() + "findByID", customerID);
		if(selectList.size()>0){
			return (CasdillyCustomer)selectList.get(0);
		}else{
			return null;
		}
		
	}



	@Override
	public void updateCustomer(CasdillyCustomer customer) {
		getSqlSession().update(this.getNs() + "updateCustomer", customer);
	}



	@Override
	public CasdillyCustomer get(String customerID) {
		return (CasdillyCustomer) getSqlSession().selectOne(this.getNs() + "get", customerID);

	}



	@Override
	public Boolean insertBatch(LinkedList<CasdillyCustomer> customerList) {
		int count = getSqlSession().insert(this.getNs() + "insertBatch", customerList);// * @return 成功操作的记录数目
		
		if (count==customerList.size()){
			return true;
		}else{
			return false;
		}
	
	}



	@Override
	public List<Map<String, String>> listByHetongSerial(Page p) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
		log.info(StringUtils.PrintCurrentDateTime("开始执行sql语句"));
		List<CasdillyCustomer> customers = this.getSqlSession().selectList(this.getNs() + "listByHetongSerial", p); 
		log.info(StringUtils.PrintCurrentDateTime("结束执行sql语句"));
		log.info(StringUtils.PrintCurrentDateTime("开始整合数据"));
		List<Map<String,String>> customermapsList = new ArrayList<Map<String,String>>(); 		
		for (CasdillyCustomer casdillyCustomer : customers) {
			List<CasdillyScanData> scandata = casdillyCustomer.getScandata();
			casdillyCustomer.setScandata(null);
			Map<String,String> map = BeanUtils.describe(casdillyCustomer);
			for (CasdillyScanData casdillyScanData : scandata) {
				map.put(casdillyScanData.getCategory()+"_"+casdillyScanData.getTypename(), casdillyScanData.getShuzhi());
			}
			customermapsList.add(map);			
		}		
		log.info(StringUtils.PrintCurrentDateTime("结束整合数据"));
		
		return customermapsList; 
	}



	@Override
	public Integer countByHetongSerial(CasdillyCustomer customer) {
		return this.getSqlSession().selectOne(this.getNs() + "countByHetongSerial", customer); 
	}



	@Override
	public List<Map<String, String>> listByHetongSerial(CasdillyCustomer customer) {
		List<CasdillyCustomer> customers = this.getSqlSession().selectList(this.getNs() + "listByHetongSerial", new Page(0, 0, customer)); 
		List<Map<String,String>> customermapsList = new ArrayList<Map<String,String>>(); 
		for (CasdillyCustomer casdillyCustomer : customers) {
			List<CasdillyScanData> scandata = casdillyCustomer.getScandata();
			casdillyCustomer.setScandata(null);
			Map<String, String> map = null;
			try {
				map = BeanUtils.describe(casdillyCustomer);
			} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
 				log.error(getTrace(e));
			}
			for (CasdillyScanData casdillyScanData : scandata) {
				if(casdillyScanData.getTypename().equals("人工判号")){
					map.put(casdillyScanData.getCategory()+"号型", casdillyScanData.getShuzhi());
				}else{
					map.put(casdillyScanData.getTypename(), casdillyScanData.getShuzhi());
				}
				
			}
			customermapsList.add(map);			
		}
		
		return customermapsList;
	}



	@Override
	public CasdillyCustomer findByFenchangIDAndUserNameAndPhone(CasdillyCustomer casdillyCustomer) {
 		return this.getSqlSession().selectOne(this.getNs() + "findByFenchangIDAndUserNameAndPhone", casdillyCustomer); 
	}



	@Override
	public List<CasdillyCustomer> listByPhone(CasdillyCustomer ct) {
		return this.getSqlSession().selectList(this.getNs() + "listByPhone", ct); 
	}



	@Override
	public CasdillyCustomer getCustomerAndScanData(String customerID) {
 		return this.getSqlSession().selectOne(this.getNs() + "getCustomerAndScanData", customerID);
	}



	@Override
	public CasdillyCustomer findByFenchangIDAndUserName(CasdillyCustomer casdillyCustomer) {
		return this.getSqlSession().selectOne(this.getNs() + "findByFenchangIDAndUserName", casdillyCustomer); 
	}



	@Override
	public List<Map<String, String>> listByIDs(String ids) throws Exception{

		List<CasdillyCustomer> customers = this.getSqlSession().selectList(this.getNs() + "listByIds", ids.split(",")); 
		List<Map<String,String>> customermapsList = new ArrayList<Map<String,String>>(); 
		for (CasdillyCustomer casdillyCustomer : customers) {
			List<CasdillyScanData> scandata = casdillyCustomer.getScandata();
			casdillyCustomer.setScandata(null);
			Map<String,String> map = BeanUtils.describe(casdillyCustomer);
			for (CasdillyScanData casdillyScanData : scandata) {
				map.put(casdillyScanData.getCategory()+"_"+casdillyScanData.getTypename(), casdillyScanData.getShuzhi());
			}
			customermapsList.add(map);			
		}
		
		return customermapsList; 
	
	}



	@Override
	public void updateCustomerExportStatus(List<Map<String, String>> list, Integer integer, Date date) {
		
		HashMap<String,Object> paramMap = new HashMap<>();
		paramMap.put("list", list);
		paramMap.put("exporterID", integer);
		paramMap.put("exportTime", date);
		
		
		
		this.getSqlSession().update(this.getNs() + "updateCustomerExportStatus", paramMap);

		
	}



	@Override
	public List<CasdillyScanData> listScandataByID(Page p) {
		return this.getSqlSession().selectList(this.getNs() + "listScandataByID", p); 
	}

	
	
	@Override
	public List<CasdillyScanData> listScandataByCustomerID(String customerID) {
		return this.getSqlSession().selectList(this.getNs() + "getScandata", customerID); 
	}
	


	@Override
	public Integer countScandataByID(String customerID) {
		return this.getSqlSession().selectOne (this.getNs() + "countScandataByID", customerID); 
	}



	@Override
	public void updateScandata(CasdillyScanData csd) {
		this.getSqlSession().update(this.getNs() + "updateScandata", csd);
	}



	@Override
	public void updateQrCodeInfo(CasdillyCustomer casdillyCustomer) {
		this.getSqlSession().update(this.getNs() + "updateQrCodeInfo", casdillyCustomer);
		
	}



	@Override
	public CasdillyCustomer findCustomerNameById(String customerID) {
		return this.getSqlSession().selectOne (this.getNs() + "findCustomerById", customerID); 
	}



	@Override
	public void updateCustomerOpenID(CasdillyCustomer customer) {
		
		this.getSqlSession().update(this.getNs() + "updateCustomerOpenID", customer);
 		
	}



	@Override
	public List<CasdillyCustomer> listBywxOpenid(String wxOpenid) {
		return this.getSqlSession().selectList(this.getNs() + "listBywxOpenid", wxOpenid); 
	}



	@Override
	public List<CasdillyCustomer> listByLinkmanList(List<Linkman> linkManList) {
		return this.getSqlSession().selectList(this.getNs() + "listByLinkmanList", linkManList); 
	}



	@Deprecated
	@Override
	public void updateDelFlagByCcustomerList(List<CasdillyCustomer> ccustomerList) {
		this.getSqlSession().update(this.getNs() + "updateDelFlagByCcustomerList", ccustomerList);
	}



	@Override
	public void updateScanDataDelFlagByCcustomerList(List<CasdillyCustomer> ccustomerList) {
		this.getSqlSession().update(this.getNs() + "updateScanDataDelFlagByCcustomerList", ccustomerList);
		
	}



	@Override
	public List<CasdillyCustomer> hodoListByHetongSerial(CasdillyCustomer customer) {
		
		List<CasdillyCustomer> customers = this.getSqlSession().selectList(this.getNs() + "listByHetongSerial", new Page(0, 0, customer)); 		 
		return customers;
	}



	@Override
	public List<CasdillyCustomer> hodoListByIDs(String ids) {
		List<CasdillyCustomer> customers = this.getSqlSession().selectList(this.getNs() + "listByIds", ids.split(",")); 
		 
		
		return customers; 
	}



	@Override
	public void updateDelInfoByCcustomerList(HashMap<String, Object> paramMap) {
		this.getSqlSession().update(this.getNs() + "updateDelInfoByCcustomerList", paramMap);
 		
	}



 


 





}
